﻿using de.wolfmarco.pas.Shared.Helper;
using de.wolfmarco.pas.Shared.Interfaces;
using de.wolfmarco.pas.Shared.MessageBoxes;
using de.wolfmarco.pas.Shared.Resources;
using Microsoft.Practices.Prism.Modularity;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Linq;
using System;
using de.wolfmarco.pas.Shared.Language;

namespace de.wolfmarco.pas.Client.MVVM
{
    /// <summary>
    /// Interaktionslogik für ShellView.xaml
    /// </summary>
    public partial class ShellView : RibbonWindow
    {
        public ShellView(IModuleCatalog moduleCatalog)
        {
            InitializeComponent();
            InitializeRibbonApplicationMenu(moduleCatalog);
        }

        private void InitializeRibbonApplicationMenu(IModuleCatalog moduleCatalog)
        {
            ModuleInfo startMenuModule = moduleCatalog.Modules
                .SingleOrDefault<ModuleInfo>(x => x.ModuleName == ClientHelper.StartMenuModule);

            addModuleToMenu(startMenuModule);

            appMenu.Items.Add(createOptionsMenuItem());
            appMenu.Items.Add(createExitMenuItem());
        }

        private void addModuleToMenu(ModuleInfo moduleInfo)
        {
            if (moduleInfo == null)
                return;

            toolBar.Items.Add(createRibbonButton(moduleInfo));
            appMenu.Items.Add(createMenuItem(moduleInfo));
        }

        private RibbonButton createRibbonButton(ModuleInfo moduleInfo)
        {
            RibbonButton ribbonButton = new RibbonButton();
            ribbonButton.SmallImageSource = Images.GetModuleImageSource(moduleInfo.ModuleName, typeof(Image16Enum));
            BindingHelper.BindElementToModule(ribbonButton, moduleInfo);

            return ribbonButton;
        }

        private RibbonApplicationMenuItem createMenuItem(ModuleInfo moduleInfo)
        {
            RibbonApplicationMenuItem menuItem = new RibbonApplicationMenuItem();
            menuItem.ImageSource = Images.GetModuleImageSource(moduleInfo.ModuleName, typeof(Image32Enum));
            menuItem.Header = ModuleDisplayNames.GetModuleDisplayName(moduleInfo);

            BindingHelper.BindElementToModule(menuItem, moduleInfo);

            return menuItem;
        }

        private RibbonApplicationMenuItem createOptionsMenuItem()
        {
            RibbonApplicationMenuItem menuItem = new RibbonApplicationMenuItem();
            menuItem.ImageSource = Images.GetImageSource(Image32Enum.App);
            menuItem.Header = "Optionen";

            BindingHelper.BindElement(menuItem, "OptionsCommand", this);

            return menuItem;
        }

        private RibbonApplicationMenuItem createExitMenuItem()
        {
            RibbonApplicationMenuItem menuItem = new RibbonApplicationMenuItem();
            menuItem.ImageSource = Images.GetImageSource(Image32Enum.Exit);
            menuItem.Header = "Beenden";

            BindingHelper.BindElementToCommand(menuItem, "ExitCommand");

            return menuItem;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            MessageBoxResult result = PASMessageBox.ShowMessageBox("Wollen Sie die Anwendung wirklich schließen?", "Anwendung schließen",
                MessageBoxButton.YesNo, MessageBoxType.Question);

            if (result == MessageBoxResult.No)
                e.Cancel = true;
        }
    }
}
